package LeetCode.leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * 两个数组的交集
 * @author: leiwenkang02@meituan.com
 * @date: 2018/8/28
 * @time: 09:52
 */
public class Test349两个数组的交集 {
    public static void main(String[] args) {
        int[] nums1 = {1, 2, 2, 1};
        int[] nums2 = {2, 2};
        int[] result = intersection(nums1, nums2);
        for (int i = 0; i < result.length; i++) {
            System.out.println(result[i]);
        }
    }

    public static int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums1.length; i++) {
            set.add(nums1[i]);
        }
        Set<Integer> interSet = new HashSet<>();

        for (int i = 0; i < nums2.length; i++) {
            if (set.contains(nums2[i])) {
                interSet.add(nums2[i]);
            }
        }
        int[] result = new int[interSet.size()];
        int j = 0;
        for (int a : interSet) {
            result[j++] = a;
        }
        return result;

    }
}
